package com.goldplusgold.tradedelay.app.trade.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.fragment.FragmentBasicDialog;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.trade.activity.ProfitAndLossActivity;
import com.goldplusgold.tradedelay.app.trade.adapter.ProfitAndLossAdapter;
import com.goldplusgold.tradedelay.app.trade.pojo.TriggeredRecord;

import java.util.ArrayList;

/**
 * Created by admin on 2017/5/27.
 */

public class DeleteDialog extends FragmentBasicDialog implements View.OnClickListener {

    private int position;

    public static DeleteDialog newInstance(int position) {

        Bundle args = new Bundle();

        DeleteDialog fragment = new DeleteDialog();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            position = args.getInt("position");
        }
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_delete_dialog, container, false);
        view.findViewById(R.id.cancel_txt).setOnClickListener(this);
        view.findViewById(R.id.conform_txt).setOnClickListener(this);
        TextView mPrice = (TextView) view.findViewById(R.id.id_text_view_price);
        TextView mHand = (TextView) view.findViewById(R.id.id_text_view_hand);
        TextView mFloatPrice = (TextView) view.findViewById(R.id.id_float_price);
        Activity activity = getActivity();
        if (activity instanceof ProfitAndLossActivity) {
            ProfitAndLossAdapter profitAndLossAdapter = ((ProfitAndLossActivity) activity).adapter;
            ArrayList<TriggeredRecord> list = profitAndLossAdapter.getArrayList();
            TriggeredRecord triggeredRecord = list.get(position);
            if (triggeredRecord != null) {
                String sltpType = triggeredRecord.getSltpType();
                if (!TextUtils.isEmpty(sltpType)) {
                    switch (sltpType) {
                        case "TP":
                            mPrice.setText("止盈价：" + triggeredRecord.getSltpPrice());
                            break;
                        case "SL":
                            mPrice.setText("止损价：" + triggeredRecord.getSltpPrice());
                            break;
                    }

                }
                mHand.setText("手数：" + triggeredRecord.getLots() + "手");
                mFloatPrice.setText("滑点价：" + triggeredRecord.getFloatPrice() + "元");
            }
        }
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_txt:
                getDialog().dismiss();
                break;
            case R.id.conform_txt:
                confirm();
                break;
        }
    }


    private void confirm() {
        Activity activity = getActivity();
        if (activity instanceof ProfitAndLossActivity) {
            ProfitAndLossAdapter profitAndLossAdapter = ((ProfitAndLossActivity) activity).adapter;
            ArrayList<TriggeredRecord> list = profitAndLossAdapter.getArrayList();
            if (list == null || list.isEmpty()) {
                getDialog().dismiss();
                return;
            }
            TriggeredRecord triggeredRecord = list.get(position);
            list.remove(position);
            profitAndLossAdapter.addEmptyItem();
            profitAndLossAdapter.notifyDataSetChanged();
            ((ProfitAndLossActivity) activity).deleteItem(triggeredRecord);

        }
        getDialog().dismiss();
    }
}
